#include "aerApplication.hpp"

#include <SFML/Window.hpp>
#include "aerEventHandler.hpp"
#include "aerWindow.hpp"

/*
#if   (AER_USED_WM == AER_SFML)
# include  "SFML/aerApplication.cpp"
#elif (AER_USED_WM == AER_SDL)
# include  "SDL/aerApplication.cpp"
#else
# error     incorrect value for macro AER_USED_WM  !
#endif
*/

namespace aer {


Application::Application()
  : m_Window(NULLPTR),
    m_bTerminated(false)
{}

Application::~Application()
{
  AER_SAFE_DELETE(m_Window);
}


Window* Application::createWindow( const Display &display, const char *title)
{
  m_Window = new Window( display, title);
  return m_Window;
}

void Application::run()
{
  AER_ASSERT( m_Window != NULLPTR );

  m_timer.start();
  init();

  while (true)
  {
    handleEvents();
    if (m_bTerminated) {
      break;
    }
    
    updateFrame();
    drawFrame();
    
    m_timer.update();
    
    m_Window->swap();
  }
}


void Application::quit() 
{ 
  m_bTerminated = true;
}


// SFML specifics
void Application::handleEvents() 
{
  sf::Event event;
    
  // Register the SFML events handler to save input
  EventHandler::Begin( &event );
  
  while (m_Window->pollEvent(event))
  {
    switch (event.type)
    {
      case sf::Event::Closed:
        quit();
        EventHandler::HandleClose();
      break;
      
      case sf::Event::Resized:
        EventHandler::HandleResize();
        m_Window->resize( event.size.width, event.size.height);//
        resize( event.size.width, event.size.height);
      break;
      
      case sf::Event::LostFocus:
        EventHandler::HandleFocus(false);
      break;
      
      case sf::Event::GainedFocus:      
        EventHandler::HandleFocus(true);
      break;
      
      case sf::Event::TextEntered:
        // Empty
      break;
      
      case sf::Event::KeyPressed:        
        EventHandler::HandleKeyboard(true);
      break;
      
      case sf::Event::KeyReleased:      
        EventHandler::HandleKeyboard(false);
      break;
      
      case sf::Event::MouseButtonPressed:
        EventHandler::HandleMouseButton(true);
      break;
      
      case sf::Event::MouseButtonReleased:
        EventHandler::HandleMouseButton(false);
      break;
      
      case sf::Event::MouseMoved:
        EventHandler::HandleMouseMoved();
      break;
      
      case sf::Event::MouseWheelMoved:
        //EventHandler::HandleWheelMoved();
      break;
      
      case sf::Event::MouseEntered:
        //EventHandler::HandleMouseFocus(true);
      break;
      
      case sf::Event::MouseLeft:
        //EventHandler::HandleMouseFocus(false);
      break;
      
      case sf::Event::JoystickButtonPressed:
        //EventHandler::HandleJoystickButton(true);
      break;
      
      case sf::Event::JoystickButtonReleased:
        //EventHandler::HandleJoystickButton(false);
      break;
      
      case sf::Event::JoystickMoved:
        EventHandler::HandleJoystickMoved();
      break;
      
      case sf::Event::JoystickConnected:
        EventHandler::HandleJoystickConnected(true);
      break;
      
      case sf::Event::JoystickDisconnected:
        EventHandler::HandleJoystickConnected(false);
      break;
      
      
      default:
      break;      
    }
  }
  
  // Unregister the SFML event handler
  EventHandler::End();
  
  // Quit the application if the window has been closed
  if (m_Window->hasClosed()) {
    quit();
  }
}

} // aer
